/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { Component } from "react";
import { DatePicker } from "antd";
import { isArray } from "lodash";
import moment from "moment";

const formatTime = "HH:mm";
const formatDay = "YYYY-MM-DD";
const formatDate = `${formatDay} ${formatTime}`;
const { RangePicker } = DatePicker;

const range = (start, end) => {
  const result = [];
  for (let i = start; i < end; i++) {
    result.push(i);
  }
  return result;
};

// 快捷转化日期：今日，昨日，三天，一周，一个月
const key2Moment = type => {
  let start = "";
  let end = moment();
  switch (type) {
    case "today":
      start = end;
      break;
    case "yesterday":
      start = end = moment().subtract(1, "days");
      break;
    case "three":
      start = moment().subtract(3, "days");
      break;
    case "week":
      start = moment().subtract(7, "days");
      break;
    case "month":
      start = moment().subtract(30, "days");
      break;
    default:
      break;
  }
  return [
    moment(`${moment(start).format(formatDay)} 00:00`, formatDate),
    moment(`${moment(end).format(formatDay)} 23:59`, formatDate),
  ];
};

// 转化日期
const moment2Date = dates => {
  if (!isArray(dates)) {
    return [];
  }
  const [start, end] = dates;
  return [`${moment(start).format(formatDate)}:00`, `${moment(end).format(formatDate)}:59`];
};

class KbRangePicker extends Component {
  static defaultProps = {
    max: 3,
    unit: "months",
    onChange: () => {},
  };

  disabledDate = current => {
    const { disabledToday = false, disableOutOfRange = false } = this.props;
    if (!current) return false;
    const { max, unit } = this.props;
    const today = moment(moment().format(formatDay));
    const yesterday = moment().subtract(1, "days");
    const select = moment(current.format(formatDay));
    if (disableOutOfRange) {
      return select > (disabledToday ? yesterday : today);
    }
    return (
      select > (disabledToday ? yesterday : today) ||
      select < moment(today.valueOf()).subtract(max, unit)
    );
  };

  onPanelChange = e => {
    const { onPickerChange } = this.props;
    if (!e && this.state) {
      const { moment } = this.state;
      onPickerChange(moment);
    }
  };

  onCalendarChange = e => {
    this.setState({
      moment: e,
    });
  };

  render() {
    const { btn_refresh, timeParams = {}, showTime } = this.props;
    const { num = 1, type = "months" } = timeParams;
    const defaultValue = [moment().subtract(num, type), moment()];
    return (
      <RangePicker
        defaultValue={defaultValue}
        disabledDate={this.disabledDate}
        format={showTime ? formatDate : formatDay}
        onCalendarChange={btn_refresh ? this.onCalendarChange : () => {}}
        onOpenChange={btn_refresh ? this.onPanelChange : () => {}}
        showTime={{
          format: formatTime,
          open: true,
          defaultValue: [moment("00:00", "HH:mm"), moment("23:59", "HH:mm")],
        }}
        style={{ width: 340 }}
        {...this.props}
      />
    );
  }
}

export default KbRangePicker;
export { formatTime, formatDay, formatDate, key2Moment, moment2Date };
